package  
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.utils.setTimeout;
	import flash.utils.Timer;
	import ginger.events.DataEvent;
	import ginger.ui.simple.SimpleBaseComponent;
	import uiLib.LeftBtn;
	import uiLib.RightBtn;
	import caurina.transitions.Tweener;
	/**
	 * ...
	 * @author Ning
	 */
	public class PhotoView extends SimpleBaseComponent
	{
		private var _data:Array;
		private var _items:Array;
		//开场预载入的图片数量
		private var _showCount:int=1;
		//最后一个出现的数据
		private var _curDataIndex:int;
		
		private var _leftBtn:LeftBtn;
		private var _rightBtn:RightBtn;
		private var _photoBox:Sprite;
		
		private var _timer:Timer;
		
		public function PhotoView() 
		{
			super();
		}
		override protected function init():void 
		{
			_items = [];
			super.init();
			_timer = new Timer(5 * 1000);
			_timer.addEventListener(TimerEvent.TIMER, onTimer);
			
			if (ExternalInterface.available) {
				ExternalInterface.addCallback('setPraise', setPraise);
			}
		}
		override protected function setupListener():void 
		{
			_leftBtn.addEventListener(MouseEvent.CLICK, prev);
			_rightBtn.addEventListener(MouseEvent.CLICK, next);

			addEventListener(MouseEvent.MOUSE_MOVE, onMove);

			
			AppGlobal.REMOTE.addEventListener(DataEvent.UPDATE, onUpdateData);
		}
		override protected function configUI():void 
		{
			super.configUI();
			
			_photoBox = new Sprite();
			addChild(_photoBox);
			
			_leftBtn = new LeftBtn();
			addChild(_leftBtn);
			
			_leftBtn.x = -105;
			_leftBtn.y = 250;
			
			
			
			_rightBtn = new RightBtn();
			addChild(_rightBtn);
			
			_rightBtn.x = 550;
			_rightBtn.y = 250;
			
			_leftBtn.alpha = 0;
			_rightBtn.alpha = 0;
			
			
			
			//创建17个子元素
			for (var i:int = 0; i < 17; i++ )
			{
				var item:PhotoItem = new PhotoItem();
				item.label = String(i);
				_items.push(item);
				if (!_photoBox.contains(item))
				    _photoBox.addChild(item);
			}
		}
		override public function show():void 
		{
			//16个元素进场
			for (var i:int = 0; i < 16; i++ )
			{
				var item:PhotoItem = _items[i] as PhotoItem;
				_photoBox.setChildIndex(item, 0);
				setTimeout(showItem, i * 130, i);
			}
			function showItem(index:int):void
			{
				for (var i:int = 0; i < index+1; i++ )
				{
					var item:PhotoItem = _items[i] as PhotoItem;
					item.showAt(index - i + 1, true);
				}
			}
			
			onOut();
			setTimeout(setupListenerByTime, 15 * 130);
			
			
			Tweener.addTween(_leftBtn, {time:1.5,delay:2.5,alpha:1,x:-155} );
			Tweener.addTween(_rightBtn, {time:1.5,delay:2.5,alpha:1,x:600 } );
		}
		
		private function setupListenerByTime():void
		{
			AppGlobal.STAGE.addEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
			addEventListener(MouseEvent.ROLL_OUT, onOut);
		}
		private function setData(data:Array):void
		{
			_data = data;
			
			for (var i:int = 0; i < _items.length-1; i++ )
			{
				var item:PhotoItem = _items[i];
				var vo:PhotoVO;
				if (i > data.length - 1) {
				    vo = PhotoVO(data[i % data.length]);
					_curDataIndex = i % data.length;
				}
				else {
				    vo = PhotoVO(data[i]);
					_curDataIndex = i;
				}
				
				item.setData(vo);
				if(i<_showCount)
				    item.addEventListener(Event.COMPLETE, onComplete);
			}
		}
		private function next(evt:MouseEvent=null):void
		{		
			if (_curDataIndex == _data.length - 1)
			    _curDataIndex = 0;
			else 
			    _curDataIndex++;
				
			for (var i:int = 0; i < _items.length; i++ )
			{
				var item:PhotoItem = _items[i];
				item.next();
				if (item.index == 1) {
				    item.setData(PhotoVO(_data[_curDataIndex]));
				}
			}
		}
		
		
		
		private function prev(evt:MouseEvent=null):void
		{
			//控制第一页不向前翻
			//小于16条数据的情况
			if (_data.length < 16) {
				for (var i:int = 0; i < _items.length; i++ )
				{
					var item:PhotoItem = _items[i];
					if (item.index == 16) {
						if (item.data == _data[0])
						    return;
						break;
					}
				}
			}
			else {
				if (_curDataIndex-15 == 0)
			    return;
			}

			_curDataIndex--;
			
			if (_curDataIndex < 0)
			    _curDataIndex = _data.length - 1;
			
			//这个主要用于小于16条数据的赋值
		    var tempPhotoItem:PhotoItem;
			
			for (i = 0; i < _items.length; i++ )
			{
				item = _items[i];
				item.prev();
				if (item.index == 16) {
					tempPhotoItem = item;
					//大于16条数据
					if (_data.length >= 16) {
						if(_curDataIndex>=15)
						    item.setData(PhotoVO(_data[_curDataIndex - 15]));
					    else 
						    item.setData(PhotoVO(_data[_curDataIndex + (_data.length-15)]));
					}
				}
			}
			if (_data.length < 16) {
				var vo:PhotoVO = getPrevData();
				tempPhotoItem.setData(vo);
			}
			
						
		}
		
		private function onWheel(evt:MouseEvent):void
		{
			if (evt.delta < 0)
			    next();
			else 
			    prev();
		}
		
		private function getPrevData():PhotoVO {
			var data:PhotoVO;
			for (var i :int= 0; i < _items.length; i++ )
			{
				var itemTemp:PhotoItem = _items[i];
				if (itemTemp.index == 15) {
					var vo:PhotoVO = itemTemp.data;
					var index:int = _data.indexOf(vo);
					index -= 1;
					if (index < 0)
					    index = _data.length - 1;
					data = _data[index];
					break;
				}
			}
			return data;
		}
		
		//完成图片载入的数量
		private var _count:int;
		private function onComplete(evt:Event):void
		{
			_count += 1;
			if (_count == _showCount)
			    show();
		}
		
		private function setPraise(id:int, value:int):void
		{
			for (var i:int = 0; i < _items.length; i++ )
			{
				var item:PhotoItem = _items[i];
				if (item.data.ID == id) {
					item.setPraise(value);
					return;
				}     
			}
			for (i = 0; i < _data.length; i++ )
			{
				var vo:PhotoVO = _data[i];
				if (vo.ID == id) {
					vo.PRAISE = value;
					return;
				}
			}
		}
		private function onTimer(evt:TimerEvent):void
		{
			next();
		}
		private function onMove(evt:MouseEvent):void
		{
			_timer.stop();
		}
		private function onOut(evt:MouseEvent=null):void
		{
			_timer.start();
		}
		
		private function onUpdateData(evt:DataEvent):void
		{
			if (_data == null) {
			    setData(evt.data as Array);
				addEventListener(Event.ENTER_FRAME, onEnterFrame);
			}
			else 
			    _data = _data.concat(evt.data as Array);
		}
		
		//此时已经判定的位置
		private var _hadIndex:int;
		//判定是否加载数据
		private function onEnterFrame(evt:Event):void
		{
			if (_data.length - _curDataIndex == 25 && _curDataIndex != _hadIndex) {
				_hadIndex = _curDataIndex;
				AppGlobal.REMOTE.updateData();
			}
		}
	}
}